﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AteneoLibriBusinessLogic.Entities;

namespace AteneoLibri.BackEnd
{
    public partial class InsertNation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
	        {
		        using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
                {
                    List<AteneoLibriBusinessLogic.Entities.Nation> nations = service.getNations();
                
                    gvNations.DataSource = nations;
                    gvNations.DataBind();
                }
	        }
            
        }

        protected void gvNations_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvNations.PageIndex = e.NewPageIndex;
            gvNations.DataBind();
        }

        protected void btnInsertNation_Click(object sender, EventArgs e)
        {
            string nationName = txtNationName.Text.Trim();
            if (nationName != null && nationName != "")
            {
                try
                {
                    Nation nation = new Nation();
                    using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
                    {
                        nation.IdNation = Guid.NewGuid();
                        nation.Description = nationName;

                        bool result = service.insertNation(nation);
                        if (result)
                        {
                            // open panel

                            // refresh page
                            Response.Redirect("InsertNation.aspx");
                        }
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

        protected void btnDeleteNation_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton button = sender as ImageButton;
            string idNation = button.CommandArgument;

            using (wsAteneoLibri.AteneoLibriServiceClient service = new wsAteneoLibri.AteneoLibriServiceClient())
            {
                Guid nationId = new Guid(idNation);
                Nation nation = service.getNation(nationId);
                List<City> cities = service.getCitiesByNationId(nationId);
                if (cities.Count() == 0)
                {
                    var result = service.deleteNation(nation);
                    if (result)
                    {
                        // refresh page
                        Response.Redirect("InsertNation.aspx");
                    }
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "Attenzione!", "alert('Cancellare prima tutte le città della nazione prima di procedere alla rimozione della nazione.');", true);
                }

                
                
            }

        }
        
    }
}